class Npc {

    dialog = new Image();

    avater = new Image();

    imageUrls: string[] = [];

    x = 0;

    y = 0;
    id = 0;
    imageId = 0;
    imageUrl = "";
    image: NpcImage;
    name = "name";

    view = new renderer.DisplayObjectContainer();
    // view = new renderer.Bitmap(0, 0, npcTexture);

    //  view = new renderer.Bitmap(0, 0, this.avater);

    // createNpc(){
    //     for(let npc of npcManager.npcs){
    //         if(thisNpc.id=)
    //     }
    // }

    canAcceptMissions: Mission[] = [];

    canSubmitMissions: Mission[] = [];
    images: NpcImage[] = [];

    constructor(config: any) {
        this.id = config.id;
        this.name = config.name;
        const avatarUrl = config.avatar;

        const avatarTexture = new Image();
        avatarTexture.src = avatarUrl;

        const bitmap = new renderer.Bitmap(0, 0, avatarTexture);
        this.view.addChild(bitmap);
        // const textField = new renderer.TextField();
        // textField.text = this.name;
        // this.view.addChild(textField);
        // textField.x = 20;
        // textField.y = 40;



        npcImageManager.parseFromConfig(config.image);
        for (let item of npcImageManager.npcImages) {
            this.imageUrls.push(item.imageUrl);
        }


        // const dialogUrl = config.image[this.imageId];
        // const dialogTexture = new Image();

        // dialogTexture.src = dialogUrl;
        // this.dialog = dialogTexture;
        // this.dialog.src = dialogUrl;

        // const dialogBitmap = new renderer.Bitmap(0, 0, dialogTexture);

        missionManager.addEventListener("missionUpdate", () => {
            this.update();
        })
        this.update();

    }

    private update() {
        this.canAcceptMissions = [];
        this.canSubmitMissions = [];
        for (let mission of missionManager.missions) {
            if (mission.status == MissionStatus.CAN_ACCEPT && mission.fromNpcId == this.id) {
                this.canAcceptMissions.push(mission);

            }
            if (mission.status == MissionStatus.CAN_SUBMIT && mission.toNpcId == this.id) {
                this.canSubmitMissions.push(mission);
            }
        }
    }


    toString() {
        return `[NPC: id:${this.id}]`
    }
}

class NpcManager {

    npcs: Npc[] = [];



    parseFromConfig(obj: any) {
        for (let item of obj.npc) {
            const npc = new Npc(item)

            this.npcs.push(npc)
        }
    }
    getNpcById(id: number) {
        for (let npc of this.npcs) {
            if (npc.id == id) {
                return npc;
            }
        }
        throw `missing npcid : ${id}`;
    }
}
const npcManager = new NpcManager();

class NpcImage {
    imageId = 0;
    imageUrl = "";


    constructor(config: any) {
        this.imageId = config.imageId;
        this.imageUrl = config.imageUrl;
    }


}
class NpcImageManager {
    npcImages: NpcImage[] = [];

    parseFromConfig(config: any) {
        for (let item of config) {
            const npcImage = new NpcImage(item);
            this.npcImages.push(npcImage);
        }

    }

    getNpcImageById(id: number) {
        for (let item of this.npcImages) {
            if (item.imageId == id) {
                return item;
            }
        }
    }
}
const npcImageManager = new NpcImageManager();